package com.htthorizon.powersupply.controller;

import cn.hutool.core.date.DatePattern;
import com.htthorizon.powersupply.dto.LineOutageItem;
import com.htthorizon.powersupply.dto.TgOutageItem;
import com.htthorizon.powersupply.dto.UserOutageItem;
import com.htthorizon.powersupply.search.TgOutageItemSearch;
import com.htthorizon.powersupply.search.UserOutageItemSearch;
import com.htthorizon.powersupply.service.UserOutageMonitorService;
import com.htthorizon.powersupply.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 用户停电监测
 */
@RestController
@RequestMapping("/userOutageMonitor")
public class UserOutageMonitorController {

    @Autowired
    private UserOutageMonitorService userOutageMonitorService;


    @GetMapping("/getRealTime")
    public R<List<LineOutageItem>> getRealTime() {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        List<LineOutageItem> items = userOutageMonitorService.getStatistics("now", dateDt);
        return R.ok(items);
    }

    @GetMapping("/getToday")
    public R<List<LineOutageItem>> getToday() {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        List<LineOutageItem> items = userOutageMonitorService.getStatistics("today", dateDt);
        return R.ok(items);
    }

    @GetMapping("/getAll")
    public R<List<LineOutageItem>> getAll() {
        List<LineOutageItem> items = userOutageMonitorService.getStatistics("all", null);
        return R.ok(items);
        //ces
    }


    @GetMapping("/getUserOutageItems")
    public R<List<UserOutageItem>> getUserOutageItems(UserOutageItemSearch search) {
        if (StringUtils.isEmpty(search.getType()) || "now".equals(search.getType())) {
            search.setType("now");
        }
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        search.setDateDt(dateDt);
        List<UserOutageItem> items = userOutageMonitorService.getUserOutageItems(search);
        return R.ok(items);
    }

    @GetMapping("/getTgOutageItems")
    public R<List<TgOutageItem>> getTgOutageItems(TgOutageItemSearch search) {
        if (StringUtils.isEmpty(search.getType()) || "now".equals(search.getType())) {
            search.setType("now");
        }
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        search.setDateDt(dateDt);
        List<TgOutageItem> items = userOutageMonitorService.getTgOutageItems(search);
        return R.ok(items);
    }
}
